<?php
/**
 * yumao
 * 2018-5-31
 * 用户登录下单打开点击频次数据统计
 */
namespace app\controllers\crontab;

use app\models\marketing\UserFrequencyBehavior;
use app\components\redis\RedisLock;
use app\components\redis\RedisMarketingEmail;
use app\models\Elog;
use yii;
class UserFrequencyController extends \app\base\Controller
{
    private $behaviorArr = [ // 行为
        'login',
        'email_open',
        'email_click',
        'order'
    ];

    /**
     * 根据当前天统计频率
     */
    public function actionStatisticsUserFrequencyByDay()
    {
        set_time_limit(0);
        ignore_user_abort(true); 
        Elog::logCrontab(__FUNCTION__, __FUNCTION__,'info');
        $mothod = strtolower(Yii::$app->request->getMethod());
        $site = Yii::$app->request->$mothod('deal_site'); 
        $behavior = Yii::$app->request->$mothod('behavior'); 
        $dealDay = Yii::$app->request->$mothod('deal_day');
        // 对当前请求加锁
        $redis = new RedisMarketingEmail();
        if (!in_array($behavior, $this->behaviorArr)) {
            exit('不支持当前行为的统计');
        }
        
        // 某个网站某种行为只允许有一个进程在统计数据
        $redisLock = new RedisLock($redis,__FUNCTION__.$behavior.$site, 3600);
       
        if ($redisLock->translock() === false) {
            $msg = 'There is a request to reject the request in the process.';
            Elog::logCrontab($msg, __FUNCTION__,'info');
            return '有请求在处理中，拒绝该请求！';
           
        }
        $webConfig = yii::$app->params['sites']['marketing_email']['web_config'];
        if (!empty($webConfig[$site])) { // 说明这个网站存在
           try {
                $userFrequencyBehavior = new UserFrequencyBehavior($site,$behavior);
                if (!empty($dealDay)) {
                    $userFrequencyBehavior->DealUserFrequencyByDay($dealDay);
                } else {
                    $userFrequencyBehavior->DealUserFrequencyByDay();
                }
              
                echo "{$site}站点，{$behavior}分天数据统计插入成功";
            } catch (\Exception $ex) {
                 $redisLock->unlock();
                $msg = "{$site}站点，{$behavior}分天数据统计插入失败，具体原因为:" . $ex->getMessage();
                echo $msg;
                Elog::logCrontab($msg,__FUNCTION__,'exception'); // 记录信息到elog
            }
        } else {
            $redisLock->unlock();
            exit('当前网站不存在');
        }
        $redisLock->unlock();
    }
    
    /**
     * 每天统计5天，10天，15天，20.....的登录，打开邮件，点击邮件，订单数
     * 
     */
    public function actionStatisticsUserFrequencyByAppointDay()
    {
        set_time_limit(0);
        ignore_user_abort(true); 
        Elog::logCrontab(__FUNCTION__, __FUNCTION__,'info');
        $mothod = strtolower(Yii::$app->request->getMethod());
        $site = Yii::$app->request->$mothod('deal_site'); 
        $behavior = Yii::$app->request->$mothod('behavior'); 
        
         // 对当前请求加锁
        $redis = new RedisMarketingEmail();
        
        $redisLock = new RedisLock($redis,__FUNCTION__, 3600);
       
        if ($redisLock->translock() === false) {
            $msg = 'There is a request to reject the request in the process.';
            Elog::logCrontab($msg, __FUNCTION__,'info');
            return '有请求在处理中，拒绝该请求！';
           
        }
        if (!in_array($behavior, $this->behaviorArr)) {
            exit('不支持当前行为的统计');
        }
        
        $webConfig = yii::$app->params['sites']['marketing_email']['web_config'];
        if (!empty($webConfig[$site])) { // 说明这个网站存在
            try {
                $userFrequencyBehavior = new UserFrequencyBehavior($site,$behavior);
                $userFrequencyBehavior->DealUserFrequencyByAppointDay();
            } catch (\Exception $ex) {
                $redisLock->unlock();
                $msg = "{$site}站点，{$behavior}多天数据统计插入失败，具体原因为:" . $ex->getMessage();
                echo $msg;
                Elog::logCrontab($msg,__FUNCTION__,'exception'); // 记录信息到elog
            } 
        } else {
            $redisLock->unlock();
            exit('当前网站不存在');
        }
        $redisLock->unlock();
    }
}